package com.union.vehicleassistant.util;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.util.TypedValue;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.union.vehicleassistant.R;
import com.union.vehicleassistant.adapter.MultiFragmentPagerAdapter;
import com.union.vehicleassistant.util.callback.SimpleCallback;
import com.union.vehicleassistant.util.display.DisplayInfoUtils;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.circlenavigator.CircleNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.List;


/**
 * 指示器工具类 {@link MagicIndicator}
 *
 * @author mz
 * @date-time 2024/11/27 14:11
 */
public class MagicIndicatorUtils {

    public static <E, F extends Fragment> CommonNavigator commonNavigator(
            MagicIndicator mi,
            ViewPager vp,
            MultiFragmentPagerAdapter<E, F> adapter,
            SimpleCallback<Integer> onClickCallback) {
        CommonNavigator navigator = new CommonNavigator(vp.getContext());
        final int padding16 = (int) DisplayInfoUtils.getInstance().dp2px(8);
        navigator.setLeftPadding(padding16);
        navigator.setRightPadding(padding16);
        navigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return adapter.getCount();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(Html.fromHtml(adapter.getPageTitle(index).toString()));
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResUtils.getDimens(R.dimen.sp_15));
                simplePagerTitleView.setSelectedColor(ResUtils.getThemeColor(context, R.attr.colorItemTitle));
                simplePagerTitleView.setNormalColor(ResUtils.getThemeColor(context, R.attr.colorItemSubTitle));
                int paddingH = (int) DisplayInfoUtils.getInstance().dp2px(8);
                int paddingV = (int) DisplayInfoUtils.getInstance().dp2px(5);
                simplePagerTitleView.setPadding(paddingH, paddingV, paddingH, paddingV);
                simplePagerTitleView.setOnClickListener(v -> {
                    vp.setCurrentItem(index);
                    if (onClickCallback != null) {
                        onClickCallback.onResult(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });
        mi.setNavigator(navigator);
        ViewPagerHelper.bind(mi, vp);
        return navigator;
    }

    public static void commonNavigator(MagicIndicator mi, ViewPager vp, PagerAdapter adapter, boolean showIndicator) {
        commonNavigator(mi, vp, adapter, showIndicator, null);
    }

    /**
     * 蓝色主题指示器
     *
     * @param mi              指示器组件
     * @param vp              ViewPager
     * @param adapter         适配器
     * @param showIndicator   是否显示指示器，默认只显示文字
     * @param onClickCallback 回调
     */
    public static void commonNavigator(MagicIndicator mi,
                                       ViewPager vp,
                                       PagerAdapter adapter,
                                       boolean showIndicator,
                                       SimpleCallback<Integer> onClickCallback) {
        CommonNavigator navigator = new CommonNavigator(vp.getContext());
        final int padding16 = (int) DisplayInfoUtils.getInstance().dp2px(8);
        navigator.setLeftPadding(padding16);
        navigator.setRightPadding(padding16);
        navigator.setAdjustMode(showIndicator);
        navigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return adapter.getCount();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(Html.fromHtml(adapter.getPageTitle(index).toString()));
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResUtils.getDimens(R.dimen.sp_15));
                simplePagerTitleView.setSelectedColor(ResUtils.getThemeColor(context, R.attr.colorTheme));
                simplePagerTitleView.setNormalColor(ResUtils.getThemeColor(context, R.attr.colorItemSubTitle));
                int paddingH = (int) DisplayInfoUtils.getInstance().dp2px(8);
                int paddingV = (int) DisplayInfoUtils.getInstance().dp2px(5);
                simplePagerTitleView.setPadding(paddingH, paddingV, paddingH, paddingV);
                simplePagerTitleView.setOnClickListener(v -> {
                    vp.setCurrentItem(index);
                    if (onClickCallback != null) {
                        onClickCallback.onResult(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                if (showIndicator) {
                    LinePagerIndicator indicator = new LinePagerIndicator(context);
                    indicator.setStartInterpolator(new AccelerateInterpolator());
                    indicator.setEndInterpolator(new DecelerateInterpolator(1.6f));
                    indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                    indicator.setLineWidth(UIUtil.dip2px(context, 10));
                    indicator.setRoundRadius(UIUtil.dip2px(context, 3));
                    indicator.setColors(ResUtils.getThemeColor(context, R.attr.colorTheme));
                    return indicator;
                }
                return null;
            }
        });
        mi.setNavigator(navigator);
        ViewPagerHelper.bind(mi, vp);
    }

    /**
     * 白色主题指示器
     *
     * @param mi              指示器组件
     * @param vp              ViewPager
     * @param adapter         适配器
     * @param onClickCallback 回调
     */
    public static void whiteNavigator(MagicIndicator mi,
                                      ViewPager vp,
                                      PagerAdapter adapter,
                                      SimpleCallback<Integer> onClickCallback) {
        CommonNavigator navigator = new CommonNavigator(vp.getContext());
        final int padding16 = (int) DisplayInfoUtils.getInstance().dp2px(8);
        navigator.setLeftPadding(padding16);
        navigator.setRightPadding(padding16);
        navigator.setAdjustMode(true);
        navigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return adapter.getCount();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(Html.fromHtml(adapter.getPageTitle(index).toString()));
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResUtils.getDimens(R.dimen.sp_15));
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(context, R.color.white));
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(context, R.color.white50));
                int paddingH = (int) DisplayInfoUtils.getInstance().dp2px(8);
                int paddingV = (int) DisplayInfoUtils.getInstance().dp2px(5);
                simplePagerTitleView.setPadding(paddingH, paddingV, paddingH, paddingV);
                simplePagerTitleView.setOnClickListener(v -> {
                    vp.setCurrentItem(index);
                    if (onClickCallback != null) {
                        onClickCallback.onResult(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(1.6f));
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineWidth(UIUtil.dip2px(context, 10));
                indicator.setRoundRadius(UIUtil.dip2px(context, 3));
                indicator.setColors(context.getResources().getColor(R.color.white));
                return indicator;
            }
        });
        mi.setNavigator(navigator);
        ViewPagerHelper.bind(mi, vp);
    }

    /**
     * 通用指示器，不适用viewpager
     *
     * @param context         上下文
     * @param mi              指示器组件
     * @param titles          标题
     * @param onClickCallback 指示器点击回调
     * @return 容器帮助类
     */
    public static FragmentContainerHelper noTitleNavigator(Context context,
                                                           MagicIndicator mi,
                                                           List<String> titles,
                                                           SimpleCallback<Integer> onClickCallback) {
        FragmentContainerHelper containerHelper = new FragmentContainerHelper();
        CommonNavigator navigator = new CommonNavigator(context);
        final int padding16 = (int) DisplayInfoUtils.getInstance().dp2px(8);
        navigator.setLeftPadding(padding16);
        navigator.setRightPadding(padding16);
        navigator.setAdjustMode(true);
        navigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(titles.get(index));
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResUtils.getDimens(R.dimen.sp_15));
                simplePagerTitleView.setSelectedColor(ResUtils.getThemeColor(context, R.attr.colorTheme));
                simplePagerTitleView.setNormalColor(ResUtils.getThemeColor(context, R.attr.colorItemSubTitle));
                int paddingH = (int) DisplayInfoUtils.getInstance().dp2px(8);
                int paddingV = (int) DisplayInfoUtils.getInstance().dp2px(5);
                simplePagerTitleView.setPadding(paddingH, paddingV, paddingH, paddingV);
                simplePagerTitleView.setOnClickListener(v -> {
                    containerHelper.handlePageSelected(index);
                    if (onClickCallback != null) {
                        onClickCallback.onResult(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(1.6f));
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineWidth(UIUtil.dip2px(context, 20));
                indicator.setRoundRadius(UIUtil.dip2px(context, 3));
                indicator.setColors(ResUtils.getThemeColor(context, R.attr.colorTheme));
                return indicator;
            }
        });
        mi.setNavigator(navigator);
        containerHelper.attachMagicIndicator(mi);
        return containerHelper;
    }

    /**
     * 固定纯标题指示器
     *
     * @param context         上下文
     * @param mi              指示器组件
     * @param titles          标题
     * @param onClickCallback 指示器点击回调
     */
    public static void fixedTitleCircleNavigator(Context context,
                                                 MagicIndicator mi,
                                                 List<String> titles,
                                                 SimpleCallback<Integer> onClickCallback) {
        FragmentContainerHelper containerHelper = new FragmentContainerHelper();
        mi.setBackgroundResource(R.drawable.bg_adjunct_tag);
        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ClipPagerTitleView clipPagerTitleView = new ClipPagerTitleView(context);
                clipPagerTitleView.setText(titles.get(index));
                clipPagerTitleView.setTextColor(ContextCompat.getColor(context, R.color.blue80));
                clipPagerTitleView.setClipColor(Color.WHITE);
                clipPagerTitleView.setOnClickListener(v -> {
                    containerHelper.handlePageSelected(index);
                    if (onClickCallback != null) {
                        onClickCallback.onResult(index);
                    }
                });
                return clipPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                float navigatorHeight = context.getResources().getDimension(R.dimen.dp_50);
                float borderWidth = UIUtil.dip2px(context, 1);
                float lineHeight = navigatorHeight - 2 * borderWidth;
                indicator.setLineHeight(lineHeight);
                indicator.setRoundRadius(lineHeight / 2);
                indicator.setYOffset(borderWidth);
                indicator.setColors(ContextCompat.getColor(context, R.color.blue80));
                return indicator;
            }
        });
        mi.setNavigator(commonNavigator);
        containerHelper.attachMagicIndicator(mi);
    }


    public static void underlineNavigator(
            Context context,
            MagicIndicator mi,
            PagerAdapter adapter,
            ViewPager vp
    ) {
        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return adapter == null ? 0 : adapter.getCount();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(adapter.getPageTitle(index));
                simplePagerTitleView.setNormalColor(Color.parseColor("#88ffffff"));
                simplePagerTitleView.setSelectedColor(Color.WHITE);
                simplePagerTitleView.setOnClickListener(v -> vp.setCurrentItem(index));
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(Color.parseColor("#40c4ff"));
                return indicator;
            }
        });
        mi.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerPadding(UIUtil.dip2px(context, 15));
        titleContainer.setDividerDrawable(ContextCompat.getDrawable(context, R.drawable.ic_edit_cursor));
        ViewPagerHelper.bind(mi, vp);
    }


    /**
     * 圆形指示器，不包含标题
     *
     * @param context   context
     * @param mi        指示器
     * @param vp        viewpager
     * @param pageCount 指示器原点数量
     */
    public static void onlyIndicator(Context context, MagicIndicator mi, ViewPager vp, int pageCount) {
        CircleNavigator circleNavigator = new CircleNavigator(context);
        circleNavigator.setCircleCount(pageCount);
        circleNavigator.setCircleColor(ContextCompat.getColor(context, R.color.blue_theme));
        circleNavigator.setCircleClickListener(vp::setCurrentItem);
        mi.setNavigator(circleNavigator);
        ViewPagerHelper.bind(mi, vp);
    }
}
